package middleware

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/config"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/uricontrol"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

// UriControl 安全校验，限制终端ip，防止暴力破解、短信攻击
func UriControl() gin.HandlerFunc {
	return func(c *gin.Context) {
		clientIp := c.ClientIP()
		uri := c.Request.RequestURI
		if err := uricontrol.UriControl(config.UriControl().Expire, config.UriControl().Max, clientIp, uri); err != nil {
			global.Log.Error("访问过快，稍后再试", zap.Any("err", err))
			c.JSON(http.StatusOK, model.Fail("访问过快，稍后再试"))
			c.Abort()
		}
		c.Next()
	}
}
